
// 扫描填充线
import { getBoundingClientRect } from './shapeRect.js'
import { segmentsIntr, edgeCrossPoints, segCrossPoints, neighbourSegCrossPoints } from './segCross.js'
import { _dis, _lerp, _o, _polar } from "../math/index.js"
import { PATH_MODE } from "../common/pathMode.js";
import { toMatrix } from '../math/array.js';
import { oppositeDirection } from '../math/vec.js'
import { rayMix } from '../math/arrayMix.js';


// 横线
export const fillHorizontalPoints = ({ points, gridDensity }) => {
    let rect = getBoundingClientRect(points)
    let { left, right, top, bottom, width, height } = rect
    let n = height / gridDensity
    let segs = []
    let ps = []
    let offset = gridDensity / 2
    for (let i = 0; i < n; i++) {
        let p = _lerp(top, bottom, i / n)
        let seg = [[left - offset, p + offset], [right + offset, p + offset]]
        segs[segs.length] = seg
    }
    let ps2 = edgeCrossPoints(points, segs)
    ps.push(...ps2)
    return ps
}
// 条纹
const _strip = (ps) => {
    let len = ps.length
    let pp = []

    for (let i = 0; i <= len - 4; i += 4) {
        let t = ps[i]
        let next = ps[(i + 1)]
        let next2 = ps[(i + 2)]
        let next3 = ps[(i + 3)]
        let oop = oppositeDirection([t, next], [next3, next2])
        if (oop) {
            pp.push(t, next, next3, next2)
        } else {
            pp.push(t, next, next2, next3)
        }
    }
    return pp
}
// 横条纹
const fillHorizontalStripPoints = ({ points, gridDensity }) => {
    let ps = fillHorizontalPoints({ points, gridDensity })
    return _strip(ps)
}

// 竖线
export const fillVerticalPoints = ({ points, gridDensity }) => {
    let rect = getBoundingClientRect(points)
    let { left, right, top, bottom, width, height } = rect
    let n = width / gridDensity
    let segs = []
    let offset = gridDensity / 2
    for (let i = 0; i < n; i++) {
        let p = _lerp(left, right, i / n)
        segs[segs.length] = [[p + offset, top - offset], [p + offset, bottom + offset]]
    }
    let ps = edgeCrossPoints(points, segs)
    return ps
}

// 竖条纹
const fillVerticalStripPoints = ({ points, gridDensity }) => {
    let ps = fillVerticalPoints({ points, gridDensity })
    return _strip(ps)
}

// 网格线
export const fillGridPoints = ({ points, gridDensity }) => {
    let ps = fillVerticalPoints({ points, gridDensity })
    let ps2 = fillHorizontalPoints({ points, gridDensity })
    return [...ps, ...ps2]
}

// 斜线
// /---backslash 反斜线 \---slash 正斜线
export const fillBackslashPoints = ({ points, gridDensity }) => {
    let rect = getBoundingClientRect(points)
    let { left, right, top, bottom, width, height } = rect
    let n = width / gridDensity
    let m = height / gridDensity
    let segs = []
    // let ps = []
    let leftTop = [left - gridDensity, top - gridDensity]
    let leftBottom = [left - gridDensity, bottom + gridDensity]
    let rightTop = [right + gridDensity, top - gridDensity]
    let rightBottom = [right + gridDensity, bottom - gridDensity]
    for (let i = 1; i <= Math.max(n, m); i++) {
        let p = _lerp(leftTop, leftBottom, i / m)
        let p2 = _lerp(leftTop, rightTop, i / n)
        segs[segs.length] = [p, p2]

        let p3 = _lerp(rightBottom, leftBottom, i / m)
        let p4 = _lerp(rightBottom, rightTop, i / n)
        segs[segs.length] = [p3, p4]
    }
    let ps = edgeCrossPoints(points, segs)
    return ps
}



// 射线
export const fillRayPoints = ({ points, rayDensity = 10 }) => {
    let o = _o(points)
    let { width } = getBoundingClientRect(points)
    let r = width
    let n = 360 / rayDensity
    let segs = []
    for (let i = 0; i < n; i++) {
        let p = _polar(o, r + 10, rayDensity * i)
        segs.push([o, p])
    }

    let ps = edgeCrossPoints(points, segs)
    return rayMix(o, ps)

}


// const fillPolarGridPoints = ({ points, rayDensity = 10 }) => {

// }


const fillRayStripPoints = ({ points }) => {
    let o = _o(points)
    let len = points.length
    let ps = []
    points.forEach((t, i) => {
        if (i % 2 === 0) {
            let next = points[(i + 1) % len]
            ps.push(o, t, next)
        }
    })
    return ps
}



const vertexLightPoints = ({ points, offset = 0.3 }) => {
    let len = points.length
    let ps = []
    points.forEach((t, i) => {
        let to = (Math.floor((len - 1) / 2) + i) % len
        let p = _lerp(points[to], points[(to + 1) % len], offset)
        ps.push(t, p)
    })
    return ps
}

const vertexLightCrossPoints = ({ points, offset = 0.3 }) => {
    let len = points.length
    let ps = []
    let segs = []
    points.forEach((t, i) => {
        let to = (Math.floor((len - 1) / 2) + i) % len
        let p = _lerp(points[to], points[(to + 1) % len], offset)
        segs.push([t, p])
    })
    let nscps = neighbourSegCrossPoints(segs)
    nscps.forEach((t, i) => {
        ps.push(points[i], t)
    })
    return ps
}

const vertexLightAreaPoints = ({ points, offset = 0.3 }) => {
    let len = points.length
    let ps = []
    let segs = []
    points.forEach((t, i) => {
        let to = (Math.floor((len - 1) / 2) + i) % len
        let p = _lerp(points[to], points[(to + 1) % len], offset)
        segs.push([t, p])
    })
    let nscps = neighbourSegCrossPoints(segs)
    nscps.forEach((t, i) => {
        let next = points[(i + 1) % len]
        ps.push(points[i], t, next)
    })
    return ps
}


// const vertexLightAreaMtrixPoints = ({ points, offset = 0.3 }) => {
//     let len = points.length
//     let ps = []
//     let segs = []
//     points.forEach((t, i) => {
//         let to = (Math.floor((len - 1) / 2) + i) % len
//         let p = _lerp(points[to], points[(to + 1) % len], offset)
//         segs.push([t, p])
//     })
//     let nscps = neighbourSegCrossPoints(segs)
//     nscps.forEach((t, i) => {
//         let next = points[(i + 1) % len]
//         ps.push([points[i], t, next])
//     })
//     return ps
// }

const edgeMidFractal=()=>{}


// horizontal stripe 横纹
// 竖线为vertical line
// 十字线

export const fillPatternPointsMap = {
    none: () => [],
    horizontal: fillHorizontalPoints,
    horizontalStrip: fillHorizontalStripPoints,
    vertical: fillVerticalPoints,
    verticalStrip: fillVerticalStripPoints,
    grid: fillGridPoints,
    backslash: fillBackslashPoints,
    ray: fillRayPoints,
    rayStrip: fillRayStripPoints,
    vertexLight: vertexLightPoints,
    vertexLightCross: vertexLightCrossPoints,
    vertexLightArea: vertexLightAreaPoints,
    // vertexLightColorArea: vertexLightAreaMtrixPoints
}

export const fillPatterns = Object.keys(fillPatternPointsMap).concat(['taichi'])

const fillPatternPathModeMap = {
    rayStrip: PATH_MODE.LINE_LOOP,
    vertexLightArea: PATH_MODE.TRIANGLES,
    // vertexLightColorArea: PATH_MODE.LINE_LOOP,
    default: PATH_MODE.LINES,
    horizontalStrip: PATH_MODE.SQUARES,
    verticalStrip: PATH_MODE.SQUARES,
    // ray: PATH_MODE.POINTS
}
export const fillPatternMap = ({ pattern, points, gridDensity, rayDensity, offset }) => {

    let fn = fillPatternPointsMap[pattern]
    let ps = fn({ points, gridDensity, rayDensity, offset })
    let pathMode = fillPatternPathModeMap[pattern] || fillPatternPathModeMap['default']

    let color
    switch (pathMode) {
        case PATH_MODE.TRIANGLES:
            ps = toMatrix(ps, 3)
            color = {
                type: 'colorCircle',
                fill: 'black'
            }
            break;
    }

    return {
        points: ps,
        pathMode,
        color,
        // labels:{}
    }

}